﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.Revit.RevitExtension;


namespace YArchitech.Revit.RevitExtension
{
    public static class RevitPrintParametersExtension
    {
        public static bool GetMaskCoincidentLines(this PrintParameters printParam)
        {
            return printParam != null && printParam.MaskCoincidentLines;
        }

        public static void SetMaskCoincidentLines(this PrintParameters printParam, bool bVal)
        {
            if (printParam == null)
            {
                return;
            }
            printParam.MaskCoincidentLines = bVal;
        }
    }
}

namespace YJKBatchPrinter
{
	public class PrintSTP : ISettingNameOperation
	{
		public PrintSTP(PrintManager printMgr, ExternalCommandData commandData)
		{
			this.m_commandData = commandData;
			this.m_printMgr = printMgr;
		}

		public Document m_doc
		{
			get
			{
				return this.m_commandData.Application.ActiveUIDocument.Document;
			}
		}

		public PrintManager getRvtPrintManger()
		{
			return this.m_printMgr;
		}

		public string PrinterName
		{
			get
			{
				return this.m_printMgr.PrinterName;
			}
		}

		public string Prefix
		{
			get
			{
				return "Default ";
			}
		}

		public int SettingCount
		{
			get
			{
				return this.m_commandData.Application.ActiveUIDocument.Document.GetPrintSettingIds().Count;
			}
		}

		public bool SaveAs(string newName)
		{
			bool result;
			try
			{
				result = this.m_printMgr.PrintSetup.SaveAs(newName);
			}
			catch (Exception ex)
			{
				PrintMgr.MyMessageBox(ex.Message);
				result = false;
			}
			return result;
		}

		public bool Rename(string name)
		{
			bool result;
			try
			{
				result = this.m_printMgr.PrintSetup.Rename(name);
			}
			catch (Exception ex)
			{
				PrintMgr.MyMessageBox(ex.Message);
				result = false;
			}
			return result;
		}

		public List<PrintSetting> PrintSetting
		{
			get
			{
				List<PrintSetting> list = new List<PrintSetting>();
				ICollection<ElementId> printSettingIds = this.m_commandData.Application.ActiveUIDocument.Document.GetPrintSettingIds();
				foreach (ElementId elementId in printSettingIds)
				{
					Element element = this.m_commandData.Application.ActiveUIDocument.Document.GetElement(elementId);
					PrintSetting printSetting = element as PrintSetting;
					if (printSetting != null && printSetting.Parameters != null)
					{
						list.Add(printSetting);
					}
				}
				return list;
			}
		}

		public List<string> PrintSettingNames
		{
			get
			{
				List<string> list = new List<string>();
				ICollection<ElementId> printSettingIds = this.m_commandData.Application.ActiveUIDocument.Document.GetPrintSettingIds();
				foreach (ElementId elementId in printSettingIds)
				{
					Element element = this.m_commandData.Application.ActiveUIDocument.Document.GetElement(elementId);
					PrintSetting printSetting = element as PrintSetting;
					if (printSetting != null && printSetting.Parameters != null)
					{
						list.Add(element.Name);
					}
				}
				list.Add("<在任务中>");
				return list;
			}
		}

		public string SettingName
		{
			get
			{
				string result = "<在任务中>";
				try
				{
					IPrintSetting currentPrintSetting = this.m_printMgr.PrintSetup.CurrentPrintSetting;
					if (currentPrintSetting != null)
					{
						PrintSetting printSetting = currentPrintSetting as PrintSetting;
						if (printSetting.IsValidObject)
						{
							result = printSetting.Name;
						}
					}
				}
				catch (Exception)
				{
				}
				return result;
			}
			set
			{
				try
				{
					if (value == "<在任务中>")
					{
						this.m_printMgr.PrintSetup.CurrentPrintSetting = this.m_printMgr.PrintSetup.InSession;
					}
					else
					{
						ICollection<ElementId> printSettingIds = this.m_commandData.Application.ActiveUIDocument.Document.GetPrintSettingIds();
						foreach (ElementId elementId in printSettingIds)
						{
							Element element = this.m_commandData.Application.ActiveUIDocument.Document.GetElement(elementId);
							if (element.Name.Equals(value))
							{
								this.m_printMgr.PrintSetup.CurrentPrintSetting = (element as PrintSetting);
							}
						}
					}
				}
				catch (Exception ex)
				{
					ex.ToString();
					MessageBox.Show("此打印机无法使用，请重新设置。", "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Hand);
				}
			}
		}

		public List<string> PaperSizes
		{
			get
			{
				List<string> list = new List<string>();
				foreach (object obj in this.m_printMgr.PaperSizes)
				{
					PaperSize paperSize = (PaperSize)obj;
					list.Add(paperSize.Name);
				}
				list.Sort();
				return list;
			}
		}

		public string PaperSize
		{
			get
			{
				string result;
				try
				{
					result = this.m_printMgr.PrintSetup.CurrentPrintSetting.PrintParameters.PaperSize.Name;
				}
				catch (Exception)
				{
					result = null;
				}
				return result;
			}
			set
			{
				foreach (object obj in this.m_printMgr.PaperSizes)
				{
					PaperSize paperSize = (PaperSize)obj;
					if (paperSize.Name.Equals(value))
					{
						this.m_printMgr.PrintSetup.CurrentPrintSetting.PrintParameters.PaperSize = paperSize;
						break;
					}
				}
			}
		}

		public List<string> PaperSources
		{
			get
			{
				List<string> list = new List<string>();
				foreach (object obj in this.m_printMgr.PaperSources)
				{
					PaperSource paperSource = (PaperSource)obj;
					list.Add(paperSource.Name);
				}
				return list;
			}
		}

		public string PaperSource
		{
			get
			{
				return this.m_printMgr.PrintSetup.CurrentPrintSetting.PrintParameters.PaperSource.Name;
			}
			set
			{
				foreach (object obj in this.m_printMgr.PaperSources)
				{
					PaperSource paperSource = (PaperSource)obj;
					if (paperSource.Name.Equals(value))
					{
						this.m_printMgr.PrintSetup.CurrentPrintSetting.PrintParameters.PaperSource = paperSource;
						break;
					}
				}
			}
		}

		public PageOrientationType PageOrientation
		{
			get
			{
				return this.m_printMgr.PrintSetup.CurrentPrintSetting.PrintParameters.PageOrientation;
			}
			set
			{
				this.m_printMgr.PrintSetup.CurrentPrintSetting.PrintParameters.PageOrientation = value;
			}
		}

		public PaperPlacementType PaperPlacement
		{
			get
			{
				return this.m_printMgr.PrintSetup.CurrentPrintSetting.PrintParameters.PaperPlacement;
			}
			set
			{
				this.m_printMgr.PrintSetup.CurrentPrintSetting.PrintParameters.PaperPlacement = value;
			}
		}

		public MarginType strToMarginType(string sType)
		{
            MarginType result = (MarginType)0;
			if (sType.Equals("无页边距"))
			{
                result = (MarginType)0;
			}
			else if (sType.Equals("打印机限制"))
			{
                result = (MarginType)1;
			}
			else if (sType.Equals("用户定义"))
			{
                result = (MarginType)2;
			}
			return result;
		}

		public string marginTypeToStr(MarginType type)
		{
			string result = null;
			if (type.Equals(0))
			{
				result = "无页边距";
			}
			else if (type.Equals(1))
			{
				result = "打印机限制";
			}
			else if (type.Equals(2))
			{
				result = "用户定义";
			}
			return result;
		}

		public Array MarginTypes
		{
			get
			{
				return Enum.GetValues(typeof(MarginType));
			}
		}

		public List<string> MarginTypesStr
		{
			get
			{
				List<string> list = new List<string>();
				Array marginTypes = this.MarginTypes;
				foreach (object obj in marginTypes)
				{
					string item = this.marginTypeToStr((MarginType)obj);
					list.Add(item);
				}
				return list;
			}
		}

		public MarginType SelectedMarginType
		{
			get
			{
				return this.m_printMgr.PrintSetup.CurrentPrintSetting.PrintParameters.MarginType;
			}
			set
			{
				this.m_printMgr.PrintSetup.CurrentPrintSetting.PrintParameters.MarginType = value;
			}
		}

		public double UserDefinedMarginX
		{
			get
			{
				return this.m_printMgr.PrintSetup.CurrentPrintSetting.PrintParameters.UserDefinedMarginX;
			}
			set
			{
				this.m_printMgr.PrintSetup.CurrentPrintSetting.PrintParameters.UserDefinedMarginX = value;
			}
		}

		public double UserDefinedMarginY
		{
			get
			{
				return this.m_printMgr.PrintSetup.CurrentPrintSetting.PrintParameters.UserDefinedMarginY;
			}
			set
			{
				this.m_printMgr.PrintSetup.CurrentPrintSetting.PrintParameters.UserDefinedMarginY = value;
			}
		}

		public HiddenLineViewsType HiddenLineViews
		{
			get
			{
				return this.m_printMgr.PrintSetup.CurrentPrintSetting.PrintParameters.HiddenLineViews;
			}
			set
			{
				this.m_printMgr.PrintSetup.CurrentPrintSetting.PrintParameters.HiddenLineViews = value;
			}
		}

		public int Zoom
		{
			get
			{
				return this.m_printMgr.PrintSetup.CurrentPrintSetting.PrintParameters.Zoom;
			}
			set
			{
				this.m_printMgr.PrintSetup.CurrentPrintSetting.PrintParameters.Zoom = value;
			}
		}

		public ZoomType ZoomType
		{
			get
			{
				return this.m_printMgr.PrintSetup.CurrentPrintSetting.PrintParameters.ZoomType;
			}
			set
			{
				this.m_printMgr.PrintSetup.CurrentPrintSetting.PrintParameters.ZoomType = value;
			}
		}

		public string RasterQualityTypeToStr(RasterQualityType rqType)
		{
			string result = null;
            if ((int)rqType == 300)
			{
				result = "高";
			}
            else if ((int)rqType == 72)
			{
				result = "低";
			}
            else if ((int)rqType == 150)
			{
				result = "中等";
			}
            else if ((int)rqType == 600)
			{
				result = "演示";
			}
			return result;
		}

		public RasterQualityType strToRasterQualityType(string sType)
		{
            RasterQualityType result = (RasterQualityType)72;
			if (sType.Equals("高"))
			{
                result = (RasterQualityType)300;
			}
			else if (sType.Equals("低"))
			{
                result = (RasterQualityType)72;
			}
			else if (sType.Equals("中等"))
			{
                result = (RasterQualityType)150;
			}
			else if (sType.Equals("演示"))
			{
                result = (RasterQualityType)600;
			}
			return result;
		}

		public Array RasterQualities
		{
			get
			{
				return Enum.GetValues(typeof(RasterQualityType));
			}
		}

		public List<string> RasterQualitiesStr
		{
			get
			{
				List<string> list = new List<string>();
				Array rasterQualities = this.RasterQualities;
				foreach (object obj in rasterQualities)
				{
					string item = this.RasterQualityTypeToStr((RasterQualityType)obj);
					list.Add(item);
				}
				return list;
			}
		}

		public RasterQualityType RasterQuality
		{
			get
			{
				return this.m_printMgr.PrintSetup.CurrentPrintSetting.PrintParameters.RasterQuality;
			}
			set
			{
				this.m_printMgr.PrintSetup.CurrentPrintSetting.PrintParameters.RasterQuality = value;
			}
		}

		public Array Colors
		{
			get
			{
				return Enum.GetValues(typeof(ColorDepthType));
			}
		}

		public List<string> ColorsStr
		{
			get
			{
				List<string> list = new List<string>();
				Array colors = this.Colors;
				foreach (object obj in colors)
				{
					string item = this.colorTypeToStr((ColorDepthType)obj);
					list.Add(item);
				}
				return list;
			}
		}

		public string colorTypeToStr(ColorDepthType cType)
		{
			string result = null;
			if (cType == null)
			{
				result = "黑白线条";
			}
            else if ((int)cType == 2)
			{
				result = "彩色";
			}
            else if ((int)cType == 1)
			{
				result = "灰度";
			}
			return result;
		}

		public ColorDepthType strToColorType(string sType)
		{
            ColorDepthType result = (ColorDepthType)2;
			if (sType.Equals("黑白线条"))
			{
                result = (ColorDepthType)0;
			}
			else if (sType.Equals("彩色"))
			{
                result = (ColorDepthType)2;
			}
			else if (sType.Equals("灰度"))
			{
                result = (ColorDepthType)1;
			}
			return result;
		}

		public ColorDepthType Color
		{
			get
			{
				return this.m_printMgr.PrintSetup.CurrentPrintSetting.PrintParameters.ColorDepth;
			}
			set
			{
				this.m_printMgr.PrintSetup.CurrentPrintSetting.PrintParameters.ColorDepth = value;
			}
		}

		public bool ViewLinksinBlue
		{
			get
			{
				return this.m_printMgr.PrintSetup.CurrentPrintSetting.PrintParameters.ViewLinksinBlue;
			}
			set
			{
				this.m_printMgr.PrintSetup.CurrentPrintSetting.PrintParameters.ViewLinksinBlue = value;
			}
		}

		public bool HideScopeBoxes
		{
			get
			{
				return this.m_printMgr.PrintSetup.CurrentPrintSetting.PrintParameters.HideScopeBoxes;
			}
			set
			{
				this.m_printMgr.PrintSetup.CurrentPrintSetting.PrintParameters.HideScopeBoxes = value;
			}
		}

		public bool HideReforWorkPlanes
		{
			get
			{
				return this.m_printMgr.PrintSetup.CurrentPrintSetting.PrintParameters.HideReforWorkPlanes;
			}
			set
			{
				this.m_printMgr.PrintSetup.CurrentPrintSetting.PrintParameters.HideReforWorkPlanes = value;
			}
		}

		public bool HideCropBoundaries
		{
			get
			{
				return this.m_printMgr.PrintSetup.CurrentPrintSetting.PrintParameters.HideCropBoundaries;
			}
			set
			{
				this.m_printMgr.PrintSetup.CurrentPrintSetting.PrintParameters.HideCropBoundaries = value;
			}
		}

		public bool HideUnreferencedViewTags
		{
			get
			{
				return this.m_printMgr.PrintSetup.CurrentPrintSetting.PrintParameters.HideUnreferencedViewTags;
			}
			set
			{
				this.m_printMgr.PrintSetup.CurrentPrintSetting.PrintParameters.HideUnreferencedViewTags = value;
			}
		}

		public bool ReplaceHalftoneWithThinLines
		{
			get
			{
				return this.m_printMgr.PrintSetup.CurrentPrintSetting.PrintParameters.ReplaceHalftoneWithThinLines;
			}
			set
			{
				this.m_printMgr.PrintSetup.CurrentPrintSetting.PrintParameters.ReplaceHalftoneWithThinLines = value;
			}
		}

		public bool MaskCoincidentLines
		{
			get
			{
				PrintParameters printParameters = this.m_printMgr.PrintSetup.CurrentPrintSetting.PrintParameters;
				return printParameters.GetMaskCoincidentLines();
			}
			set
			{
				PrintParameters printParameters = this.m_printMgr.PrintSetup.CurrentPrintSetting.PrintParameters;
				printParameters.SetMaskCoincidentLines(value);
			}
		}

		public bool Save()
		{
			bool result;
			try
			{
				result = this.m_printMgr.PrintSetup.Save();
			}
			catch (Exception ex)
			{
				PrintMgr.MyMessageBox(ex.Message);
				result = false;
			}
			return result;
		}

		public void Revert()
		{
			try
			{
				this.m_printMgr.PrintSetup.Revert();
			}
			catch (Exception ex)
			{
				PrintMgr.MyMessageBox(ex.Message);
			}
		}

		public bool Delete()
		{
			bool result;
			try
			{
				bool flag = false;
				if (this.m_printMgr.PrintSetup.Delete())
				{
					this.m_doc.Regenerate();
					flag = true;
					int count = this.PrintSetting.Count;
				}
				result = flag;
			}
			catch (Exception ex)
			{
				PrintMgr.MyMessageBox(ex.Message);
				result = false;
			}
			return result;
		}

        public bool VerifyMarginType(System.Windows.Forms.Control controlToEnableOrNot)
		{
            return controlToEnableOrNot.Enabled = ((int)this.m_printMgr.PrintSetup.CurrentPrintSetting.PrintParameters.PaperPlacement == 1);
		}

        public bool VerifyUserDefinedMargin(Collection<System.Windows.Forms.Control> controlsToEnableOrNot)
		{
            bool flag = (int)this.m_printMgr.PrintSetup.CurrentPrintSetting.PrintParameters.MarginType == 2;
            foreach (System.Windows.Forms.Control control in controlsToEnableOrNot)
			{
				control.Enabled = flag;
			}
			return flag;
		}

		public bool VerifyRevitEdition(int nRvtEdition)
		{
			int num = Convert.ToInt32(this.m_commandData.Application.Application.VersionNumber);
			return nRvtEdition == num;
		}

		private ExternalCommandData m_commandData;

		private PrintManager m_printMgr;
	}
}
